﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Security.Cryptography.X509Certificates;
using Newtonsoft.Json;
using Cloud.Core.Model;
using Cloud.Core.Response;
using Cloud.Core.AuthUser;
using Microsoft.EntityFrameworkCore.Internal;
using Cloud.Core.Bizlogic;
using Cloud.Core.ModelSearch;
using System.IO.Ports;
using System.Threading;

namespace Cloud.Core.Controllers
{
    /// <summary>
    /// 数据查询
    /// </summary> 
    [Route("api/v1/[controller]")]
    [ApiController]
    public class DataInquiryController : ControllerBase
    {
        private readonly CloudDataBase _context;
        private readonly ModOperate.Mod _thisModel = ModOperate.Mod.MBasicInfo;
        public DataInquiryController(CloudDataBase context)
        {
            _context = context;
        }

        #region base
        #region 日志记录查询
        /// <summary>
        /// 日志记录查询
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="operaType"></param>
        /// <returns></returns>
        [Route("LogRecordSearch")]
        [HttpGet]
        public async Task<JsonResponse<DataList<Log>>> LogRecordSearch([FromHeader] string Authorization, DateTime startDate, DateTime endDate, string LogType)
        {
            var _character = Online.GetLoginInfo(Authorization);
            var response = Online.AuthUser<DataList<Log>>(_thisModel, ModOperate.Operate.Search, _character);
            //if (response.Status != ResponseStatus.Success)
            //{
            //    return response;
            //}
            if (startDate == null || endDate == null)
            {
                response.Msg = "日期和模块名称不能为空";
                response.Status = ResponseStatus.ParameterError;
                return response;
            }
            startDate = Convert.ToDateTime($"{startDate.ToString("yyyy-MM-dd")} 00:00:01");
            endDate = Convert.ToDateTime($"{endDate.ToString("yyyy-MM-dd")} 23:23:59");
            response.Data = new DataList<Log>();
            var listQuery = _context.Log
                                    .Where(c => c.OperateTime >= startDate)
                                    .Where(c => c.OperateTime <= endDate);
            if (!string.IsNullOrEmpty(LogType))
            {
                listQuery = listQuery.Where(c => (int)c.LogType == Convert.ToInt32(LogType));
            }

            response.Data.Total = await listQuery.CountAsync().ConfigureAwait(false);
            var list = await listQuery
                   .OrderByDescending(t => t.OperateTime)
                   .ToArrayAsync()
                   .ConfigureAwait(false);
            response.Data.Count = list.Length;
            response.Data.List = list;
            response.Msg = "查询成功";
            response.Status = ResponseStatus.Success;

            return response;
        }
        #endregion
        #endregion


    }
}
